#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Last Update:

'''docstrin g
'''
__revision__ = '0.1'
__author__ = 'lxd'
import sys
sys.path.append('.')
sys.path.append('..')
import webBrowser
import wx
import config
from panel import setup
from workThread import WorkThread
import wxHelper

class MainFrame(webBrowser.MainFrame):
    def __init__(self, log, name=u'QQ七雄争霸专用浏览器', icon_name=r'static/bin.ico', **kargs):
        webBrowser.MainFrame.__init__(self, log, name, icon_name,  **kargs)
        self.work = None
        self.btnQQ7X = wx.Button(self, -1, u'七雄争霸')
        self.Bind(wx.EVT_BUTTON, self.OnQQ7XClick, self.btnQQ7X)  
        self.lblBlank = wx.StaticText(self, -1, ' '*8)
        self.btnSetup = wx.Button(self, -1, u'游戏设置')
        self.Bind(wx.EVT_BUTTON, self.OnSetupClick, self.btnSetup)  
        self.btnStart = wx.Button(self, -1, u'开始')
        self.Bind(wx.EVT_BUTTON, self.OnStartClick, self.btnStart)  
        self.btnQuit = wx.Button(self, -1, u'停止')
        self.btnQuit.Enabled = False
        self.Bind(wx.EVT_BUTTON, self.OnQuitClick, self.btnQuit)  
        self.layout()
        self.setupDialog = None

    def layout(self):
        self.urlSizer.Add(self.btnQQ7X, 0, wx.ALL, 5)
        self.toolSizer.Add(self.lblBlank, 0, wx.ALL, 5)
        self.toolSizer.Add(self.btnSetup, 0, wx.ALL, 5)
        self.toolSizer.Add(self.btnStart, 0, wx.ALL, 5)
        self.toolSizer.Add(self.btnQuit, 0, wx.ALL, 5)

    def OnQQ7XClick(self, evt):
        url = config.game_url
        self.txtUrl.SetValue(url)
        self.ie.LoadUrl(url)

    def OnSetupClick(self, evt):
        if self.setupDialog:
            self.setupDialog.Close()

        if self.bindFlash():
            data = config.loadData()
            frame = setup.MainFrame(self.log, self.dm, data=data)
            frame.Show()

    def bindFlash(self):
        b = BindFlash(self.log, self.dm)
        if b.bind():
            return True
        else:
            wxHelper.msg(u'请先进入游戏')
            self.work = None
            self.btnStart.Enabled = True
            self.btnQuit.Enabled = False
            return False

    def OnStartClick(self, evt):
        if not self.work:
            if self.bindFlash():
                data = config.loadData()
                self.work = WorkThread(self, **data)
                self.work.setDaemon(True) 
                self.work.start()

                self.btnStart.Enabled = False
                self.btnQuit.Enabled = True

    def OnQuitClick(self, evt):
        if self.work:
            self.work.stop()
            self.work = None
        self.btnStart.Enabled = True
        self.btnQuit.Enabled = False

import copy
import platform

class BindFlash(object):
    def __init__(self, log, dm):
        self.log = log
        self.dm = dm

    def bind(self):
        if self.getFlashHwnd():
            self.dm.bindWindow()
            self.setDict()
            return True
        return False

    def getFlashHwnd(self):
        if self.dm.getWindowClass() == 'MacromediaFlashPlayerActiveX':
            self.log.debug('find MacromediaFlashPlayerActiveX')
            return True
        else:
            dm = copy.copy(self.dm)
            dm.getSonByClass('AtlAxWin').getSon().getSon().getSon().getSon()
            if dm.getWindowClass() == 'MacromediaFlashPlayerActiveX':
                self.dm = dm
                self.log.info('find flash success')
                return True
            else:
                return False

    def setDict(self):
        songti9 = self.__getSysFont(platform.release())
        dicts = [songti9]#宋体9号数字 + 英文 + 简体汉字 + 中英文常见字符，去掉了`'.
        self.dm.setDict(dicts)
        self.dm.useDict(0)

    def __getSysFont(self, system):
        if system == 'post2008Server':
            font = r'static/songti9_7.txt' 
        elif system == '2003Server':
            font = r'static/songti9_2003.txt' 
        elif system == 'XP':#TODO:待验证
            font = r'static/songti9_2003.txt' 
        else:
            self.log.error('平台型号：%s' %system)
            font = ''
        return font

if __name__ == '__main__':
    app = wx.PySimpleApp()
    main = MainFrame(config.log)
    main.Show()
    app.MainLoop()

